import examService from '@/services/exam/exam';
import sugarCallBack from '@/libs/sugar/callback/index';
import {isEmpty} from '@/utils/index';

let {
	judgeCallBack
} = sugarCallBack;

const examStore = {
	namespaced: true,
	//全局单例，响应式
	state: {
		param: {
			examType: -1, //考试类型
			examId: undefined, //考试Id
			paperId: undefined, //试卷id
			ascription: undefined, //归属 3：公安，2：执法
			amount: undefined, //题量
			pointType: undefined, //知识点类型
			recordId:undefined,//结果id
			taskUserJoinId:undefined,
			taskId:undefined,
			resultType:undefined
		},
		examData: undefined, //试题数据
		curSubject: {}, //当前题目
		curIndex: -1, //当前索引
		usedTime:0,//答题总用时
		service: new examService()
	},
	//获取状态属性
	getters: {

	},
	//同步修改
	mutations: {
		//初始化参数,后面保存结果和再来一套时使用
		initParam(state, payload) {
			// console.log("初始化参数：", payload);
			state.param = payload;
			if(isEmpty(state.param.resultType)){
				state.param.resultType = payload.examType;
			}
		},
		updateParam(state, payload){
			if(!isEmpty(payload.examId)){
				state.param.examId = payload.examId
			}
			if(!isEmpty(payload.paperId)){
				state.param.paperId = payload.paperId
			}
		},
		updateUsedTime(state, payload){
			state.usedTime = payload
		},
		//初始化考试数据
		initExamData(state, payload) {
			state.examData = payload;
			// console.log("数据", state.examData)
		},
		//修改索引
		updateIndex(state, payload) {
			state.curIndex = payload;
			state.curSubject = state.examData.subjectDetail[payload];
			// console.log("当前选项："+state.curSubject.selectChoice)
		},
		//修改当前题目选择选择项
		updateSubjectChoice(state, payload) {
			const choiceType = state.curSubject.choiceType;
			state.curSubject.subjectChoices.find((item) => {
				if (payload.indexOf(item.choiceId) > -1) {
					item.isChose = '1'
				} else {
					item.isChose = '0'
				}
			});
			// console.log("state:",payload);
			state.curSubject.selectChoice = payload;
		},
		//修改当前题目用时
		updateSubjectDuration(state, payload){
			state.curSubject.useDuration = payload;
		},
		//resetExamStore
		resetExamStore(state, payload){
			state.curIndex = -1;
		}

	},
	//异步修改：有耗时操作时，调用此模块
	actions: {
		async getExam({
			dispatch,
			commit,
			state
		}, payload = {}) {
			// console.log('请求数据：', payload);
			const response = await state.service.getExam(state.param);
			if(response.success){
				if(response.code!=202){
					commit('initExamData', response.result);
					const quitNum = response.result.noFinishSubjectNum;
					if (quitNum != undefined && quitNum != '') {
						commit('updateIndex', parseInt(quitNum))
					} else {
						commit('updateIndex', 0)
					}
					commit('updateParam',{examId:response.result.examId,paperId:response.result.paperId})					
				}
				judgeCallBack(payload.callback, response);
			}else{
				uni.showToast({
					title:response.message,
					icon:'none',
					duration:2000
				})
			}
		},
		async saveExamResult({
			dispatch,
			commit,
			state
		}, payload = {}) {
			//循环得到提交数据
			const {
				examId,examType,
				paperId,recordId,taskUserJoinId,resultType
			} = state.param;
			let saveResult = {
				examId: examId,
				paperId: paperId,
				usedTime: state.usedTime,
				isFinished: payload.isFinished, //是否答完 1 :未完成  2：已完成
				quitNum: state.curIndex, //当前退出题号
				examResult: [],
				taskUserJoinId: taskUserJoinId,
				resultType: resultType
			};
			if(!isEmpty(recordId)){
				saveResult.recordId = recordId
			}
			//未作答题目个数
			let noAnswerNum = 0;
			for (let i = 0; i < state.examData.subjectDetail.length; i++) {
				let item = state.examData.subjectDetail[i]
				let selectedChoice = [];
				if(item.selectChoice == undefined||item.selectChoice.length==0){
					noAnswerNum++;
				}else{
					selectedChoice = item.selectChoice
				}
				const useDuration = item.useDuration == undefined || item.useDuration == '' ? "0" : item.useDuration	
				let resultItem = {
					selectChoice: selectedChoice,
					subjectId: item.subjectId,
					duration: useDuration
				}
				saveResult.examResult.push(resultItem);
			}
			if(!payload.isForceSubmit&&payload.isFinished==2&&noAnswerNum>0){
				uni.showModal({
					content:"您还有"+noAnswerNum+"题未作答，请全部答完再提交",
					showCancel:false
				})
			}else{
				uni.$emit('onSheet',{optionType:'submit'});
				saveResult.usedTime = state.usedTime;
				//console.log('保存结果数据：',JSON.stringify(saveResult));
				state.service.saveExamResult(saveResult).then((response) => {
					if(response.success){//模拟考试保存成功后，通知列表状态改变
						const updateParam = {
							recordId:response.result.recordId,
							isFinished:payload.isFinished,
							examId:examId
						};
						console.log('send','examFinish');
						if('5'==examType){
							uni.$emit('examFinishHistory',updateParam);
						}
						uni.$emit('examFinish',updateParam);
					}
					judgeCallBack(payload.callback,response);
				})
			}
		}
	},
	modules: {}
};
export default examStore;
